﻿using System;
using System.Collections.Generic;
using System.Web.Http;

using aier.business.logic;
using aier.dataaccess.model;

using log4net;

namespace aier.wx.api.selfhost.Controllers
{
    /// <summary>
    /// 筛查
    /// </summary>
    [Authorize]
    [RoutePrefix("api/Screen")]
    public class ScreenController : ApiController
    {
        /// <summary>
        /// 日志
        /// </summary>
        public ILog log { get; set; }
        /// <summary>
        /// 逻辑
        /// </summary>
        public ScreenBll screenBll { get; set; }
        /// <summary>
        /// 综合逻辑
        /// </summary>
        public CommonBll commonBll { get; set; }

        /// <summary>
        /// 工具类
        /// </summary>
        public ApiTool apiTool { get; set; }

        /// <summary>
        /// 查询所有筛查设备
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [ActionName("QueryAllEquipment")]
        public IHttpActionResult QueryAllEquipment()
        {
            return Ok(screenBll.QueryAllEquipment());
        }



        /// <summary>
        /// 根据筛查id获取筛查信息
        /// </summary>
        /// <param name="screenId">筛查id</param>
        /// <returns>筛查信息</returns>
        [HttpGet]
        [ActionName("GetScreenByScreenID")]
        public IHttpActionResult GetScreenByScreenID(string screenId) {
            try
            {
                if (!string.IsNullOrWhiteSpace(screenId))
                {
                    var screen = screenBll.FetchScreenByScreenID(new Guid(screenId));
                    return Ok(screen);
                }

                return NotFound();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);

            }
        }

        /// <summary>
        /// 创建筛查
        /// 如果对像中userid有值，则模仿userid用户操作，反之使用默认账号操作
        /// </summary>
        /// <param name="screen">筛查对像</param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult Post([FromBody] Screen screen)
        {
            try
            {
                ResResult resResult = screenBll.CreateScreen(screen);
                return Ok<ResResult>(resResult);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 创建筛查
        /// 如果对像中userid有值，则模仿userid用户操作，反之使用默认账号操作
        /// </summary>
        /// <param name="screen">筛查对像</param>
        /// <returns></returns>
        [HttpPost]
        [ActionName("CreateScreenData")]
        public IHttpActionResult CreateScreenData([FromBody] Screen screenData)
        {
            try
            {
                ResResult resResult = commonBll.CreateScreenEquimentsAssociate(screenData);
                return Ok<ResResult>(resResult);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 更新筛查
        /// 如果对像中userid有值，则模仿userid用户操作，反之使用默认账号操作
        /// 原程序 :ScreenInfoServiceImpl-> modScreenState(String screenid, Integer screenstate,DoctorUser doctorUser)
        /// </summary>
        /// <param name="screen">筛查对像</param>
        /// <returns></returns>
        [HttpPost]
        [ActionName("UpdateScreenData")]
        public IHttpActionResult UpdateScreenData([FromBody] Screen screen)
        {
            try
            {
                if(!Guid.Empty.Equals(screen.new_screenId))
                {
                    ResResult resResult = commonBll.UpdateScreenEquimentsAssociate(screen);
                    return Ok<ResResult>(resResult);
                }
                return NotFound();
               
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 获取所有筛查活动
        /// 原程序方法 ：ScreeningServiceImpl-> findAllScInfos   
        /// </summary>
        /// <param name="pageIndex">当前页索引</param>
        /// <param name="pageSize">页面显示条数</param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetAllScreen(int pageIndex, int pageSize)
        {
            try
            {
                var screenView = screenBll.GetAllScreen(pageIndex, pageSize);
                return Ok<IEnumerable<ScreenView>>(screenView);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 查询当前用户相关的筛查排期列表,负责人或组员里包含登录用户
        /// 原程序方法：ScreeningServiceImpl-> findScpqByUsername(String username)
        /// <param name="domainname">域账号</param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetScreenByDomainName(string domainname)
        {
            try
            {
                IEnumerable<Screen_PQView> pQViews = screenBll.GetScreenByDomainName(domainname);
                return Ok<IEnumerable<Screen_PQView>>(pQViews);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 分页查询当前用户相关的筛查排期列表,负责人或组员里包含登录用户
        /// 原程序方法：ScreeningServiceImpl->findScpqByUsername(String username, int fromIndex, int toIndex)
        /// <param name="domainname">域账号</param>
        /// <param name="pageNum">当前页索引</param>
        /// <param name="pageSize">页面显示条数</param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [ActionName("GetScreenByDomainNamePaging")]
        public IHttpActionResult GetScreenByDomainName(int pageNum, int pageSize)
        {
            try
            {
                UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                string domainname = userInfo.domainname;
                var views =  screenBll.GetScreenByDomainName(domainname, pageNum, pageSize);
                return Ok<PageResult<Screen_PQView>>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }
        /// <summary>
        /// 去掉校园筛查
        /// </summary>
        /// <param name="pageNum"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [ActionName("GetScreenByDomainNamePaging1")]
        public IHttpActionResult GetScreenByDomainName1(int pageNum, int pageSize)
        {
            try
            {
                UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                string domainname = userInfo.domainname;
                var views = screenBll.GetScreenByDomainName1(domainname, pageNum, pageSize);
                return Ok<PageResult<Screen_PQView>>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 分页查询当前用户相关的筛查排期列表(不包括大客户和大学校园筛查)
        /// 原程序方法：ScreeningServiceImpl-> findScpqNoBigByUsername(String username, int fromIndex, int toIndex)
        /// </summary>
        /// <param name="domainname">域账号</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <param name="pageSize">页面显示条数</param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetScreenNoBigDomainNamePaging")]
        public IHttpActionResult GetScreenNoBigDomainName(string domainname, int pageIndex, int pageSize)
        {
            try
            {
                var views = screenBll.GetScreenNoBigDomainName(domainname, pageIndex, pageSize);
                return Ok<PageResult<Screen_PQView>>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 查询当前用户相关的筛查排期列表,负责人或组员里包含登录用户
        /// 原程序方法：ScreeningServiceImpl-> findScpqByUsername(String username, String scState) 
        /// </summary>
        /// <param name="domainname">域账号</param>
        /// <param name="scState">筛查状态</param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetScreenByDomainNameAndState(string domainname, string scState)
        {
            try
            {
                var views = screenBll.GetScreenByDomainNameAndState(domainname, scState);
                return Ok<IEnumerable<Screen_PQView>>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 分页查询当前用户相关的筛查排期列表,负责人或组员里包含登录用户
        /// 原程序方法：ScreeningServiceImpl-> findScpqByUsername(String username, String scState, int fromIndex, int toIndex)
        /// </summary>
        /// <param name="scState">筛查状态</param>
        /// <param name="pageNum">当前页索引</param>
        /// <param name="pageSize">页面大小</param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [ActionName("GetScreenByDomainNameAndStatePaging")]
        public IHttpActionResult GetScreenByDomainNameAndState(string scState, int pageNum, int pageSize)
        {
            try
            {
                UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                string domainname = userInfo.domainname;
                var views = screenBll.GetScreenByDomainNameAndState(domainname, scState, pageNum, pageSize);
                return Ok<PageResult<Screen_PQView>>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }
        /// <summary>
        /// 去掉校园筛查
        /// </summary>
        /// <param name="scState"></param>
        /// <param name="pageNum"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [ActionName("GetScreenByDomainNameAndStatePaging1")]
        public IHttpActionResult GetScreenByDomainNameAndState1(string scState, int pageNum, int pageSize)
        {
            try
            {
                UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                string domainname = userInfo.domainname;
                var views = screenBll.GetScreenByDomainNameAndState1(domainname, scState, pageNum, pageSize);
                return Ok<PageResult<Screen_PQView>>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 分页查询当前用户相关的筛查排期列表(不包括大客户和大学校园筛查)
        /// 原程序方法：ScreeningServiceImpl-> findScpqNoBigByUsername(String username, String scState, int fromIndex, int toIndex)
        /// </summary>
        /// <param name="domainname">域账号</param>
        /// <param name="scState">筛查状态</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <param name="pageSize">页面大小</param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetScreenNoBigDomainNameAndStatePaging")]
        public IHttpActionResult GetScreenNoBigDomainNameAndState(string domainname, string scState, int pageIndex, int pageSize)
        {
            try
            {
                var views = screenBll.GetScreenNoBigDomainNameAndState( domainname,  scState,  pageIndex,  pageSize);
                return Ok<PageResult<Screen_PQView>>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 根据客户ID查询该客户所有参与的筛查信息
        /// 原程序方法：ScreeningServiceImpl-> findScInfosByCustomerId(String customerId)  ,findScInfosByLeadId(String leadId)
        /// </summary>
        /// <param name="customerid">客户ID</param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetScreenByCustomerId(Guid customerid)
        {
            try
            {
                var views = screenBll.GetScreenByCustomerId(customerid);
                return Ok<IEnumerable<Screen_Info_For_AccountView>>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 根据姓名,电话，身份证模糊查询CRM客户信息
        /// 原程序方法：ScreeningServiceImpl-> selectAierAccountCustomerinfos(DoctorUser user, String key)
        /// </summary>
        /// <param name="userInfo">用户信息</param>
        /// <param name="condition">姓名或电话或身份证</param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetAccountCustomerInfoByConditionPaging([FromUri] UserInfo userInfo, string condition, int pageIndex, int pageSize)
        {
            try
            {
                var views = screenBll.GetAccountCustomerInfoByCondition(userInfo, condition, pageIndex,  pageSize);
                return Ok<PageResult<Account_CustomerInfoView>>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 统计页面查询客户列表 姓名或手机或身份证模糊查询
        /// 原程序方法：ScreeningServiceImpl-> statistics4CustomerList(String teamid, String startdate, String enddate, String qryType, String key) 
        /// </summary>
        /// <param name="teamid">筛查组ID</param>
        /// <param name="startdate">预约创建日期</param>
        /// <param name="enddate">预约创建日期</param>
        /// <param name="qryType">查询类型："tohospital"（到院）,"sign"（签约手术）,"operate"（手术）,"notoperate"（非手术）</param>
        /// <param name="condition">姓名或手机或身份证模糊查询</param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetAccountCustomerInfoByTeamIdAndCreatedOnAndCondition(string teamid, string startdate, string enddate, string qryType, string condition)
        {
            try
            {
                var views = screenBll.GetAccountCustomerInfoByTeamIdAndCreatedOnAndCondition(teamid, startdate, enddate, qryType, condition);
                return Ok<IEnumerable<Account_CustomerInfoView>>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 根据客户id查询CRM单个客户信息
        /// 原程序方法：ScreeningServiceImpl-> selectAierAccountCustomerinfoByLeadId(String leadid) 
        /// </summary>
        /// <param name="leadid">客户id</param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetAccountCustomerInfoByLeadId(Guid leadid)
        {
            try
            {
                var views = screenBll.GetAccountCustomerInfoByLeadId(leadid);
                return Ok<Account_CustomerInfoView>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 根据客户id查询CRM单个客户信息
        /// 原程序方法：ScreeningServiceImpl-> queryCustomerInfoByTaskid(String taskid)
        /// </summary>
        /// <param name="taskid">客户id</param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetAccountCustomerInfoByTaskId(Guid taskid)
        {
            try
            {
                var views = screenBll.GetAccountCustomerInfoByTaskId(taskid);
                return Ok<Account_CustomerInfoView>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        ///  查询大客户活动合作项目
        ///  原程序 ScreeningServiceImpl-> findJointProjectInfo(int projectCategory, int projectPhase)
        /// </summary>
        /// <param name="projectCategory">项目类别</param>
        /// <param name="projectPhase">项目状态</param>
        [HttpGet]
        public IHttpActionResult QueryJoinProjectByCategoryAndPhase(int projectCategory, int projectPhase)
        {

            try
            {
                var views = screenBll.QueryJoinProjectByCategoryAndPhase(projectCategory, projectPhase);
                return Ok<IEnumerable<JoinprojectView>>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 查询筛查检查项目模板
        /// 原程序：ScreeningServiceImpl-> listNewTScPageElement(String name, String ownerid, Integer statecode)
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="ownerid">负责人ID</param>
        /// <param name="statecode">状态</param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult QueryPageElementByNameAndOwnerIdAndStateCode(string name, string ownerid, int statecode)
        {
            try
            {
                var views = screenBll.QueryPageElementByNameAndOwnerIdAndStateCode(name, ownerid, statecode);
                return Ok<IEnumerable<T_SC_Page_ElementView>>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 营销资源设备查询
        /// 原程序：ScreeningServiceImpl-> listEquipment(String name, Integer type, Integer statecode)
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="type">资源分类</param>
        /// <param name="statecode">状态</param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult QueryEquipmentViewByNameAndType(string name, int type, int statecode)
        {
            try
            {
                var views = screenBll.QueryEquipmentViewByNameAndType(name, type, statecode);
                return Ok<IEnumerable<EquipmentView>>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 营销资源设备查询 根据设备ID查询，id间用','号分开
        /// 原程序：ScreenInfoServiceImpl-> qryNewEquipmentsByEqids(String eqids)
        /// </summary>
        /// <param name="eqids">id间用','号分开</param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetEquipmentViewById")]
        public IHttpActionResult QueryEquipmentViewById(string eqids)
        {
            try
            {
                var views = screenBll.QueryEquipmentViewById(eqids);
                return Ok<IEnumerable<EquipmentView>>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }


        /// <summary>
        /// 根据筛查ID,查询营销资源设备
        /// 原程序 ：ScreenInfoServiceImpl-> qryEquipmentIdsByScreenid(String screenid, DoctorUser doctorUser)
        /// </summary>
        /// <param name="screenId">筛查ID</param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetEquipmentIdsByScreenId")]
        public IHttpActionResult QueryEquipmentIdsByScreenId(string screenId)
        {
            try
            {
                var views = screenBll.QueryEquipmentIdsByScreenId(screenId);
                return Ok<string>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 获取所有筛查检查项目
        /// 原程序 ：ScItemServiceImpl->  getAll()
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetAllTSCItem")]
        public IHttpActionResult GetScreenAllSCItem()
        {
            try
            {
                var views = screenBll.GetScreenAllSCItem();
                return Ok<IEnumerable<T_SC_ItemView>>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }

        }

        /// <summary>
        /// 根据ItemId获取筛查检查项目
        /// 原程序 ：ScItemServiceImpl-> AierTScItem getById(String itemid)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetTSCItemByItemId")]
        public IHttpActionResult GetScreenSCItemByItemId(string itemId)
        {
            try
            {
                var views = screenBll.GetScreenSCItemByItemId(itemId);
                return Ok<T_SC_ItemView>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 根据id查询筛查信息与设备资源
        /// 原程序 ：ScreeningServiceImpl-> ScreenInfo4WS qryScreenInfo(String screenid, DoctorUser doctorUser)
        /// </summary>
        /// <param name="screenId">筛查信息id</param>
        /// <returns>单条筛查信息</returns>
        [HttpGet]
        [ActionName("GetScreenViewByScreenID")]
        public IHttpActionResult QueryScreenViewByScreenID(Guid screenId)
        {
            try
            {
                var views = screenBll.QueryScreenViewByScreenID(screenId);
                return Ok<ScreenView>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 创建筛查、筛查设备关系
        /// 原程序 :ScreenInfoServiceImpl-> addScreenInfo(ScreenInfo4WS screenInfo4WS, DoctorUser doctorUser) 
        /// </summary>
        /// <param name="screen">筛查对像</param>
        /// <param name="equipments">筛查设备关系</param>
        /// <returns></returns>
        public IHttpActionResult CreateScreenEquimentsAssociate([FromUri]Screen screen, [FromUri]IEnumerable<ScreenEquipmentView> equipments)
        {
            try
            {
                var views = commonBll.CreateScreenEquimentsAssociate( screen,equipments);
                return Ok<ResResult>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 签到
        /// </summary>
        /// <param name="screen">筛查对像</param>
        /// <param name="equipments">筛查设备关系</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [ActionName("CheckIn")]
        public IHttpActionResult CheckIn(Screen screen)
        {
            try
            {
                if (screen!=null)
                {
                    UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                    Guid userid = new Guid(userInfo.userid.ToString());
                    screen.new_checkinperson = userid;
                    screen.userid = userid;
                    screen.new_screenstate = 5;
                    var views = screenBll.UpdateScreen(screen);
                    return Ok<ResResult>(views);
                }
                return NotFound();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 更新筛查、筛查设备关系
        /// 原程序 :ScreenInfoServiceImpl-> modScreenInfo(ScreenInfo4WS screenInfo4WS, DoctorUser doctorUser) 
        /// </summary>
        /// <param name="screen">筛查对像</param>
        /// <param name="equipments">筛查设备关系</param>
        /// <returns></returns>
        public IHttpActionResult UpdateScreenEquimentsAssociate([FromUri]Screen screen, [FromUri]IEnumerable<ScreenEquipmentView> equipments)
        {
            try
            {
               if(!Guid.Empty.Equals( screen.new_screenId))
                {
                    var views = commonBll.UpdateScreenEquimentsAssociate(screen, equipments);
                    return Ok<ResResult>(views);
                }
                return NotFound();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        [HttpGet]
        [ActionName("GetScreenItemData")]
        public IHttpActionResult GetScreenItemData(Guid leadId)
        {
            try
            {
                if (!Guid.Empty.Equals(leadId))
                {
                    var data= screenBll.GetScreenItemData(leadId);
                    return Ok(data);
                }
                return NotFound();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 保存宣教信息，更新筛查客户信息
        /// 原程序 ：ScreenInfoServiceImpl-> saveScPreach(DoctorUser user, TScPreach scPreach)
        /// </summary>
        /// <param name="customer">筛查客户</param>
        /// <param name="propaganda">筛查宣教</param>
        /// <returns></returns>
        public IHttpActionResult SaveScCustomerPropaganda(ScreenCustomer customer, ScreenPropaganda propaganda)
        {
            try
            {
                var views = commonBll.SaveScCustomerPropaganda( customer,  propaganda);
                return Ok<ResResult>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 保存宣教信息，更新筛查客户，潜客信息
        /// 原程序 ：ScreenInfoServiceImpl-> Result saveScPreach(DoctorUser user, TScPreach scPreach, String sc_customerid, Short insurancetype, Short insurancelocation,String remark)
        /// </summary>
        /// <param name="new_sc_customerid">筛查客户ID</param>
        /// <param name="lead">潜客</param>
        /// <param name="propaganda">宣教</param>
        /// <returns></returns>
        public IHttpActionResult SaveScLeadPropaganda(Guid new_sc_customerid, LeadInfo lead, ScreenPropaganda propaganda)
        {
            try
            {
                var views = commonBll.SaveScLeadPropaganda(new_sc_customerid, lead, propaganda);
                return Ok<ResResult>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 社区筛查单人模式录入
        /// 原程序：ScreenInfoServiceImpl-> addSQSC(DoctorUser user, TLeadBase tLeadBase, TScCustomer tScCustomer, List TScResult  tScResults, TScPreach tScPreach, ReservationInfo reservationInfo)
        /// </summary>
        /// <param name="sqsc">社区筛查单人模式对像</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [ActionName("CreateSQSC")]
        public IHttpActionResult CreateSQSC([FromBody]CreateSCOBJ sqsc)
        {

            try
            {
                UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                Guid userid = new Guid(userInfo.userid.ToString());
                sqsc.lead.userid = userid;
                var views = commonBll.CreateSQSC(sqsc,userInfo);
                return Ok<ResResult>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sqscCRM"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionName("CreateSQSCCRM")]
        public IHttpActionResult CreateSQSCCRM([FromBody]CreateSCOBJCRM sqscCRM)
        {

            try
            {
                CreateSCOBJ sqsc = new CreateSCOBJ();
                sqsc.lead = sqscCRM.lead;
                sqsc.customer = sqscCRM.customer;
                sqsc.reservation = sqscCRM.reservation;
                sqsc.propaganda = sqscCRM.propaganda;
                sqsc.CreateResults = sqscCRM.CreateResults;
                UserInfo userInfo = sqscCRM.user;
                Guid userid = new Guid(userInfo.userid.ToString());
                sqsc.lead.userid = userid;
                var views = commonBll.CreateSQSC(sqsc, userInfo);
                return Ok<ResResult>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 社区筛查单人模式修改
        /// 原程序 ：ScreenInfoServiceImpl-> modSQSC(DoctorUser user, TLeadBase tLeadBase, TScCustomer tScCustomer, List<TScResult> insertResults, List<TScResult> modifyResults, TScPreach tScPreach, ReservationInfo reservationInfo)
        /// </summary>
        /// <param name="sqsc">社区筛查单人模式对像</param>
        /// <returns></returns>
        /// 已作废
        [HttpPost]
        [Authorize]
        [ActionName("UpdateSQSC")]
        public IHttpActionResult UpdateSQSC(SCOBJ sqsc)
        {
            try
            {
                UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                Guid userid = new Guid(userInfo.userid.ToString());
                sqsc.lead.userid = userid;
                sqsc.customer.userid = userid;
                var views = commonBll.UpdateSQSC(sqsc);
                return Ok<ResResult>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 社区筛查单人模式修改
        /// 原程序 ：ScreenInfoServiceImpl-> modSQSC(DoctorUser user, TLeadBase tLeadBase, TScCustomer tScCustomer, List<TScResult> insertResults, List<TScResult> modifyResults, TScPreach tScPreach, ReservationInfo reservationInfo)
        /// </summary>
        /// <param name="sqsc">社区筛查单人模式对像</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [ActionName("UpdateSQSCGroup")]
        public IHttpActionResult UpdateSQSCGroup(SCOBJ sqsc)
        {
            try
            {
                UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                Guid userid = new Guid(userInfo.userid.ToString());
                if(sqsc.reservation!=null && sqsc.reservation.userid==Guid.Empty)
                {
                    sqsc.reservation.userid = userid;
                }
                sqsc.lead.userid = userid;
                sqsc.customer.userid = userid;
                var views = commonBll.UpdateSQSCGroup(sqsc);
                return Ok<ResResult>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 校园查单人模式录入
        /// 原程序 ：ScreenInfoServiceImpl-> addXYSC(DoctorUser user, TLeadBase tLeadBase, TScCustomer tScCustomer, List<TScResult> tScResults, TScPreach tScPreach, ReservationInfo reservationInfo)
        /// </summary>
        /// <param name="sqsc">校园查单人模式对像</param>
        /// <returns></returns>
        [HttpPost]
        [ActionName("CreateXYSC")]
        public IHttpActionResult CreateXYSC(SCOBJ sqsc)
        {
            try
            {
                var views = commonBll.CreateXYSC(sqsc);
                return Ok<ResResult>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 校园筛查单人模式修改
        /// 原程序 ：modXYSC(DoctorUser user, TLeadBase tLeadBase, TScCustomer tScCustomer, List<TScResult> insertResults, List<TScResult> modifyResults, TScPreach tScPreach, ReservationInfo reservationInfo)
        /// </summary>
        /// <param name="sqsc">校园查单人模式对像</param>
        /// <returns></returns>
        [HttpPost]
        [ActionName("UpdateXYSC")]
        public IHttpActionResult UpdateXYSC(SCOBJ sqsc)
        {

            try
            {
                var views = commonBll.UpdateXYSC(sqsc);
                return Ok<ResResult>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 团队模式信息采集
        /// </summary>
        /// <param name="sqsc"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [ActionName("CreateSQXXCJ")]
        public IHttpActionResult CreateSQXXCJ(SCOBJ sqsc)
        {

            try
            {
                UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                Guid userid = new Guid(userInfo.userid.ToString());
                sqsc.lead.userid = userid;
                var views = commonBll.SaveScreenCustomer(sqsc.lead,sqsc.customer.new_scid,sqsc.customer.new_screenteamid, sqsc.customer.new_scitemid);
                return Ok<ResResult>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }
        /// <summary>
        /// 创建教宣
        /// </summary>
        /// <param name="screenPropagandaOBJ"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [ActionName("CreatePropagandaAndReservation")]
        public IHttpActionResult CreatePropagandaAndReservation(ScreenPropagandaOBJ screenPropagandaOBJ)
        {
            try
            {
                UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                //screenPropagandaOBJ.reservation.new_hospital = userInfo.siteid;
                //screenPropagandaOBJ.reservation.new_reservationdate = screenPropagandaOBJ.reservation.new_reservationdate.Value.ToUniversalTime();
                var views = commonBll.CreatePropagandaAndReservation(screenPropagandaOBJ, userInfo);
                return Ok<ResResult>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 按筛查组获取筛统计信息
        /// </summary>
        /// <param name="screenteamid">筛查组ID</param>
        /// <param name="startDate">开始时间</param>
        /// <param name="endDate">结果时间</param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [ActionName("QryScreenStatisticsBy")]
        public IHttpActionResult QryScreenStatisticsBy(Guid screenteamid, string startDate, string endDate)
        {
            try
            {
                var views = screenBll.QryScreenStatisticsBy(screenteamid, startDate, endDate);
                return Ok<ScreenStatisticsView>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }
    }
}
